package com.blb.zycommunity.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.zycommunity.entity.ResponseResult;
import com.blb.zycommunity.entity.SysLogininfor;
import com.blb.zycommunity.entity.vo.SysLogininforExeclWrite;
import com.blb.zycommunity.entity.vo.SysLogininforVo;
import com.blb.zycommunity.entity.vo.SysOperLogExcelWrite;
import com.blb.zycommunity.mapper.SysLogininforMapper;
import com.blb.zycommunity.service.ISysLogininforService;
import com.blb.zycommunity.utils.ExcelDownloadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 系统访问记录 前端控制器
 * </p>
 *
 * @author chenheng
 * @since 2023-11-20
 */
@RestController
public class SysLogininforController {

    @Resource
    private SysLogininforMapper sysLogininforMapper;

    @Autowired
    private ISysLogininforService iSysLogininforService;

    /**
     * 查询加分页
     */
    @PostMapping("login-page")
    public ResponseResult findPageAll(@RequestBody SysLogininforVo sysLogininforVo){
        Page<SysLogininfor> page = new Page<>(sysLogininforVo.getPageNum(),sysLogininforVo.getPageSize());

        Page<SysLogininfor> sysLogininforPage = sysLogininforMapper.selectLoginAllPage(page, sysLogininforVo.getIpaddr(),
                sysLogininforVo.getUserName(), sysLogininforVo.getStatus(), sysLogininforVo.getStartTime(),
                sysLogininforVo.getFinishTime());
        return ResponseResult.ok(sysLogininforPage);
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @DeleteMapping("/login/{ids}")
    public ResponseResult deleteById(@PathVariable Long[] ids){
        for (Long id : ids) {
            iSysLogininforService.removeById(id);
        }
        return ResponseResult.ok("删除成功");
    }


    /**
     * 根据id导出数据
     * @param response
     * @param ids
     * @param filename
     * @throws IOException
     */
    @GetMapping("downloads-login")
    public void exports(
            HttpServletResponse response,
            @RequestParam("ids") long[] ids,
            @RequestParam("filename") String filename) throws IOException {
        System.out.println(filename);
        List<SysLogininfor> sysLogininfors = new ArrayList<>();
        for (Long id : ids) {
            SysLogininfor byId = iSysLogininforService.getById(id);
            sysLogininfors.add(byId);
        }
        sysLogininfors.forEach(System.out::println);
        ExcelDownloadUtil.excelDownload(response, filename, SysLogininforExeclWrite.class, sysLogininfors);
    }


}
